package com.github.shuaidd.service.mapper;

import com.github.shuaidd.model.entity.ModelEntity;
import com.github.shuaidd.model.repository.ModelEntityMapper;
import com.github.shuaidd.model.repository.ModelRelationEntityMapper;
import lombok.AllArgsConstructor;
import org.activiti.app.domain.editor.Model;
import org.activiti.app.domain.editor.ModelInformation;
import org.activiti.app.domain.editor.ModelRelation;
import org.activiti.engine.impl.cmd.AbstractCustomSqlExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 描述 ModelEntityMapper 实现类
 *
 * @author ddshuai
 * @date 2020-07-18 10:30
 **/
@AllArgsConstructor(onConstructor_ = {@Autowired})
@Service
public class ModelEntityService extends BaseMapperService implements ModelEntityMapper {

    @Override
    public int deleteByPrimaryKey(String id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.deleteByPrimaryKey(id);
            }
        });
    }

    @Override
    public int insert(Model record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.insert(record);
            }
        });
    }

    @Override
    public int insertSelective(Model record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.insert(record);
            }
        });
    }

    @Override
    public Model selectByPrimaryKey(String id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Model>(ModelEntityMapper.class) {
            @Override
            public Model execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.selectByPrimaryKey(id);
            }
        });
    }

    @Override
    public Model selectByModelKey(String modelKey) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Model>(ModelEntityMapper.class) {
            @Override
            public Model execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.selectByModelKey(modelKey);
            }
        });
    }

    @Override
    public int updateByPrimaryKeySelective(Model record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.updateByPrimaryKeySelective(record);
            }
        });
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(Model record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.updateByPrimaryKeyWithBLOBs(record);
            }
        });
    }

    @Override
    public int updateByPrimaryKey(ModelEntity record) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.updateByPrimaryKey(record);
            }
        });
    }

    @Override
    public List<Model> findModelsByParentModelId(String id) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<Model>>(ModelEntityMapper.class) {
            @Override
            public List<Model> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findModelsByParentModelId(id);
            }
        });
    }

    @Override
    public List<Model> findModelsCreatedBy(String userId, int modelTypeApp, String filter, String orderBy) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<Model>>(ModelEntityMapper.class) {
            @Override
            public List<Model> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findModelsCreatedBy(userId, modelTypeApp, filter, orderBy);
            }
        });
    }

    @Override
    public List<ModelRelation> findByParentModelIdAndType(String parentModelId, String relationType) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<ModelRelation>>(ModelEntityMapper.class) {
            @Override
            public List<ModelRelation> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findByParentModelIdAndType(parentModelId, relationType);
            }
        });
    }

    @Override
    public int deleteModelRelation(ModelRelation relation) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.deleteModelRelation(relation);
            }
        });
    }

    @Override
    public List<Model> findModelsByKeyAndType(String key, Integer modelType) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<Model>>(ModelEntityMapper.class) {
            @Override
            public List<Model> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findModelsByKeyAndType(key, modelType);
            }
        });
    }

    @Override
    public List<Model> findModelsByModelType(int modelType, String validFilter) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<Model>>(ModelEntityMapper.class) {
            @Override
            public List<Model> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findModelsByModelType(modelType, validFilter);
            }
        });
    }

    @Override
    public List<ModelInformation> findParentModels(String modelId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, List<ModelInformation>>(ModelEntityMapper.class) {
            @Override
            public List<ModelInformation> execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.findParentModels(modelId);
            }
        });
    }

    @Override
    public int deleteModelRelationsForParentModel(String parentModelId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.deleteModelRelationsForParentModel(parentModelId);
            }
        });
    }

    @Override
    public String appDefinitionIdByModelAndUser(String modelId, String userId) {
        return managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, String>(ModelEntityMapper.class) {
            @Override
            public String execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.appDefinitionIdByModelAndUser(modelId,userId);
            }
        });
    }

    public Model save(Model model) {
        model.setId(String.valueOf(System.currentTimeMillis()));
        managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelEntityMapper, Integer>(ModelEntityMapper.class) {
            @Override
            public Integer execute(ModelEntityMapper modelEntityMapper) {
                return modelEntityMapper.insert(model);
            }
        });
        return model;
    }

    public void saveRelation(ModelRelation relation) {
        managementService.executeCustomSql(new AbstractCustomSqlExecution<ModelRelationEntityMapper, Integer>(ModelRelationEntityMapper.class) {
            @Override
            public Integer execute(ModelRelationEntityMapper modelEntityMapper) {
                return modelEntityMapper.insert(relation);
            }
        });
    }
}
